
package com.idealighter.game.games.texaspoker.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.dictionary.domain.RobotConfigDomain;
import com.idealighter.game.dictionary.domain.TexaporkerRoomDomain;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.games.texaspoker.TexaspokerPlayer;
import com.idealighter.game.games.texaspoker.message.ReqReadyMsg;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(505224)
public class ResBanlanceHandler implements ResMessageHandler {

  private static final Logger LOG = LoggerFactory.getLogger(ResBanlanceHandler.class);

  @Override
  public void action(Player player, ResMessage message) {
    // ResBanlanceMsg msg = (ResBanlanceMsg) message;
    TexaspokerPlayer texaspokerPlayer = (TexaspokerPlayer) player;
    texaspokerPlayer.resetData();
    // msg.getBillInfo().stream().filter(billInfo -> billInfo.getOrder() ==
    // gameStrategy.seat(player.playerId).getOrder()).forEach(billInfo -> {
    // gameStrategy.member(player.playerId).setChips(gameStrategy.member(player.playerId).getChips()
    // + billInfo.getBet());
    // });

    MemInfo memInfo = texaspokerPlayer.member(player.playerId);


    TexaporkerRoomDomain roomDomain = ((TexaspokerPlayer) player).roomDomain;

    // 校验玩家身上的钱换成的筹码是否足够(最小筹码)
    if (memInfo.getChips() < roomDomain.getMinOne()) {
      LOG.info("[德州扑克]玩家[{}][{}]身上的筹码小于最小筹码", player.userName, player.playerId);
      player.delayLogout();
      return;
    }

    RobotConfigDomain robotCfg = player.configDomain;
    // 超过最大携带金币需要退出房间
    if (convertGold(roomDomain, memInfo.getChips()) > robotCfg.getMaxGold()) {
      LOG.info("[德州扑克]玩家[{}][{}]身上的金币超过最大携带金币", player.userName, player.playerId,
          robotCfg.getMaxGold());
      player.delayLogout();
      return;
    }

    // 超过最大游戏局数
    if (texaspokerPlayer.totalGameNum-- < 1) {
      LOG.info("[德州扑克]玩家[{}][{}]超过总游戏局数退出游戏...", player.userName, player.playerId);
      player.delayLogout();
      return;
    }

    // 超过换桌局数
    if (texaspokerPlayer.tableGameNum-- < 1) {
      texaspokerPlayer.tableGameNum =
          RandCodeUtil.random(robotCfg.getTableGameLower(), robotCfg.getTableGameUpper());
      LOG.info("[德州扑克]玩家[{}][{}]超过换桌局数换桌...", player.userName, player.playerId);
      player.delayLogout();
      return;
    }
    // 游戏结束后继续准备
    player.schedule(() -> player.sendMsg(new ReqReadyMsg()), RandCodeUtil.random(3, 5),
        TimeUnit.SECONDS);
  }

  /**
   * 筹码换算金币.
   * 
   * @param roomDomain .
   * @param chips .
   */
  public long convertGold(TexaporkerRoomDomain roomDomain, long chips) {

    return chips * roomDomain.getProportionGold() / roomDomain.getProportionChips();
  }
}
